package edu.its.dreamteam.web.pages.admin;

import java.util.List;

import org.apache.tapestry.ValueEncoder;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.ioc.annotations.Inject;

import edu.its.dreamteam.daf.beans.Club;
import edu.its.dreamteam.daf.beans.Position;
import edu.its.dreamteam.daf.dao.IClubDao;
import edu.its.dreamteam.daf.dao.IPlayerDao;
import edu.its.dreamteam.daf.dao.IPositionDao;
import edu.its.dreamteam.web.pages.model.ClubSelectionModel;
import edu.its.dreamteam.web.pages.model.PositionSelectionModel;
import edu.its.dreamteam.web.util.ClubValueEncoder;
import edu.its.dreamteam.web.util.PositionValueEncoder;

public class ClubSearch {
	private List<Position> positionList;
	private List<Club> clubList;
	
	@Inject
	private IPositionDao positionDao;
	@Inject
	private IClubDao clubDao;
	@Inject
	private IPlayerDao playerDao;
	
	
	@Persist("session")
	private String searchFirstName;
	@Persist("session")
	private String searchLastName;
	
	@Persist("session")
	private String searchPosition;
	@Persist("session")
	private String searchClub;
	
	@InjectPage
	private PlayerGrid playerGrid;
	
	public IPositionDao getPositionDao() {
		return positionDao;
	}
	public IClubDao getClubDao() {
		return clubDao;
	}
	public IPlayerDao getPlayerDao() {
		return playerDao;
	}	
	
	public String getSearchFirstName() {
		return searchFirstName;
	}
	public void setSearchFirstName(String searchFirstName) {
		this.searchFirstName = searchFirstName;
	}
	public String getSearchLastName() {
		return searchLastName;
	}
	public void setSearchLastName(String searchLastName) {
		this.searchLastName = searchLastName;
	}
	
	public String getSearchPosition() {
		return searchPosition;
	}
	public void setSearchPosition(String searchPosition) {
		this.searchPosition = searchPosition;
	}
	public String getSearchClub() {
		return searchClub;
	}
	public void setSearchClub(String searchClub) {
		this.searchClub = searchClub;
	}
	
	public List<Position> getPositionList() {
		positionList = getPositionDao().queryForList("", "");
		return positionList;
	}
	public List<Club> getClubList() {
		clubList = getClubDao().queryForList("", "");
		return clubList;
	}
	
	public PositionSelectionModel getPositionSelectionModel() {		
		return new PositionSelectionModel(getPositionList());
	}
	public ValueEncoder<Position> getPositionValueEncoder(){
		return new PositionValueEncoder(getPositionList());
	} 
	public ClubSelectionModel getClubSelectionModel() {		
		return new ClubSelectionModel(getClubList());
	}
	public ValueEncoder<Club> getClubValueEncoder(){
		return new ClubValueEncoder(getClubList());
	} 
	
	void onSuccessFromSearchPlayerForm(){
		List playerList = getPlayerDao().queryForList("\"FIRST_NAME\" LIKE '%"+getSearchFirstName()+"%'" +
													  "AND \"LAST_NAME\" LIKE '%"+getSearchLastName()+"%'","");
		playerGrid.setPlayerList(playerList);
		
	}
}
